package com.Servlet;

import com.google.gson.Gson;
import com.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AjaxServlet extends BaseServlet{
    protected void javaScriptAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ajax请求过来了");
        Person person = new Person(1,"张三");
        //客户端服务器不在同一台电脑上，需要将数据转为字符串才能进行数据传输，所以需要将Person对象转为json格式的字符串
        Gson gson = new Gson();
        String personJsonString = gson.toJson(person);
        resp.getWriter().write(personJsonString);
    }

    protected void jQueryAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQueryAjax方法调用了");
        Person person = new Person(1,"张三");
        //客户端服务器不在同一台电脑上，需要将数据转为字符串才能进行数据传输，所以需要将Person对象转为json格式的字符串
        Gson gson = new Gson();
        String personJsonString = gson.toJson(person);
        resp.getWriter().write(personJsonString);
    }


    protected void jQueryGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQueryGet方法调用了");
        Person person = new Person(1,"张三");
        //客户端服务器不在同一台电脑上，需要将数据转为字符串才能进行数据传输，所以需要将Person对象转为json格式的字符串
        Gson gson = new Gson();
        String personJsonString = gson.toJson(person);
        resp.getWriter().write(personJsonString);
    }

    protected void jQueryPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQueryPost方法调用了");
        Person person = new Person(1,"张三");
        //客户端服务器不在同一台电脑上，需要将数据转为字符串才能进行数据传输，所以需要将Person对象转为json格式的字符串
        Gson gson = new Gson();
        String personJsonString = gson.toJson(person);
        resp.getWriter().write(personJsonString);
    }

    protected void jQueryGetJSON(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQueryGetJSON方法调用了");
        Person person = new Person(1,"张三");
        //客户端服务器不在同一台电脑上，需要将数据转为字符串才能进行数据传输，所以需要将Person对象转为json格式的字符串
        Gson gson = new Gson();
        String personJsonString = gson.toJson(person);
        resp.getWriter().write(personJsonString);
    }

    protected void jQuerySerialize(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQuerySerialize方法调用了");
        System.out.println("用户名"+req.getParameter("username"));
        System.out.println("密码"+req.getParameter("password"));
        Person person = new Person(1,"张三");
        //客户端服务器不在同一台电脑上，需要将数据转为字符串才能进行数据传输，所以需要将Person对象转为json格式的字符串
        Gson gson = new Gson();
        String personJsonString = gson.toJson(person);
        resp.getWriter().write(personJsonString);
    }
}
